package com.amoyt.project.service.admin.impl;

import com.amoyt.project.config.redis.RedisIdWork;
import com.amoyt.project.constants.IdConstants;
import com.amoyt.project.constants.ResponseConstants;
import com.amoyt.project.domain.bo.LoginUser;
import com.amoyt.project.domain.dto.insert.AlleviationAddDto;
import com.amoyt.project.domain.dto.query.AlleviationPlanPageQuery;
import com.amoyt.project.domain.dto.update.AlleviationUpdateDto;
import com.amoyt.project.domain.pojo.AlleviationPlan;
import com.amoyt.project.domain.res.PageResult;
import com.amoyt.project.domain.res.Result;
import com.amoyt.project.mapper.AlleviationPlanMapper;
import com.amoyt.project.service.admin.AlleviationPlanService;
import com.amoyt.project.utils.ConvertBeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 脱贫计划表 服务实现类
 * </p>
 *
 * @author AmoyT
 * @since 2025-08-21
 */
@Service
public class AlleviationPlanServiceImpl implements AlleviationPlanService {

    @Autowired
    private AlleviationPlanMapper alleviationPlanMapper;
    @Autowired
    private RedisIdWork redisIdWork;

    @Override
    public Result list(AlleviationPlanPageQuery query) {
        //分页查询
        IPage<AlleviationPlan> page = new Page<>(query.getPage(), query.getPageSize());

        //条件
        QueryWrapper<AlleviationPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(query.getHouseholdName() != null, "household_name", query.getHouseholdName());
        queryWrapper.eq(query.getPovertyType() != null, "poverty_type", query.getPovertyType());
        queryWrapper.between(query.getStartTime() != null && query.getEndTime() != null, "plan_start_date", query.getStartTime(), query.getEndTime());
        queryWrapper.like(query.getResponsiblePerson() != null, "responsible_person", query.getResponsiblePerson());
        queryWrapper.eq(query.getProgress() != null, "progress", query.getProgress());

        IPage<AlleviationPlan> queryPage = alleviationPlanMapper.selectPage(page, queryWrapper);

        PageResult pageResult = PageResult.builder()
                .total(queryPage.getTotal())
                .records(queryPage.getRecords())
                .build();

        return Result.success(pageResult);
    }

    @Override
    public Result update(AlleviationUpdateDto updateDto) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        AlleviationPlan alleviationPlan = ConvertBeanUtils.convert(updateDto, AlleviationPlan.class);
        alleviationPlan.setUpdateBy(loginUser.getUser().getUserId());
        alleviationPlan.setUpdateTime(LocalDateTime.now());

        alleviationPlanMapper.updateById(alleviationPlan);
        return Result.success(ResponseConstants.UPDATE_SUCCESS);
    }

    @Override
    public Result add(AlleviationAddDto addDto) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        AlleviationPlan alleviationPlan = ConvertBeanUtils.convert(addDto, AlleviationPlan.class);

        //获取id
        String planId = IdConstants.PLAN_ID_PREFIX + redisIdWork.nextId(IdConstants.PLAN_ID_PREFIX);
        alleviationPlan.setPlanId(planId);
        alleviationPlan.setCreateTime(LocalDateTime.now());
        alleviationPlan.setCreateBy(loginUser.getUser().getUserId());

        alleviationPlanMapper.insert(alleviationPlan);

        return Result.success(ResponseConstants.ADD_SUCCESS);
    }

    @Override
    public Result delete(List<String> ids) {
        alleviationPlanMapper.deleteBatchIds(ids);
        return Result.success(ResponseConstants.DELETE_SUCCESS);
    }
}
